<?php
namespace App\Intelihelp\Controller;

class mailController extends \Foxframe\Controller
{
	public function __construct()
	{
		parent::__construct();
		$this->view->setTheme('mailIntelihelp');
		$this->view->addTemplateDir(APP_PATH.'Intelihelp/View/mail');
	}

	public function tarefaInteracao($params = array())
	{
		$this->view->assign('tarefa',	$params['tarefa']);
		$this->view->assign('anotacao',	$params['anotacao']);
		$this->view->assign('pessoa',	$params['pessoa']);

		$content	= $this->view->fetch('tarefaInteracao.html');
		$mail = new \Foxframe\Mail();
		$mail->Subject		= utf8_decode("[Intelihelp] Nova interação - Tarefa {$params['tarefa']->getIdTarefa()}");
		$mail->Body			= $this->view->renderTheme($content);
		$mail->MessageID	= $params['tarefa']->getIdTarefa();

		$stakeholders = $this->em->getRepository('\Entities\Intelihelp\Tarefa')->findStakeholders($params['tarefa']);
		foreach ($stakeholders as $email)
		{
			$mail->AddAddress($email['ds_email'], utf8_decode($email['ds_nome']));
		}

		if(!$mail->Send())
		{
		   return true;
		}
		return false;
	}

	public function tarefaCadastrada($params = array())
	{
		$this->view->assign('tarefa', $params['tarefa']);

		$content	= $this->view->fetch('tarefaCadastrada.html');
		$mail = new \Foxframe\Mail();
		$mail->Subject		= utf8_decode("[Intelihelp] Nova Tarefa #{$params['tarefa']->getIdTarefa()}");
		$mail->Body			= $this->view->renderTheme($content);

		$stakeholders = $params['tarefa']->getEquipe()->getPessoas();
		foreach ($stakeholders as $pessoa)
		{
			foreach ($pessoa->getEmails() as $email)
			{
				$mail->AddAddress($email->getDsEmail(), utf8_decode($pessoa->getDsNome()));
			}
		}

		if(!$mail->Send())
		{
		   return true;
		}
		return false;
	}

	public function tarefaAtualizada($params = array())
	{
		$this->view->assign('tarefa', $params['tarefa']);

		$content	= $this->view->fetch('tarefaAtualizada.html');
		$mail = new \Foxframe\Mail();
		$mail->Subject		= utf8_decode("[Intelihelp] A Tarefa #{$params['tarefa']->getIdTarefa()} foi atualizada");
		$mail->Body			= $this->view->renderTheme($content);

		$stakeholders = $params['tarefa']->getEquipe()->getPessoas();
		foreach ($stakeholders as $pessoa)
		{
			foreach ($pessoa->getEmails() as $email)
			{
				$mail->AddAddress($email->getDsEmail(), utf8_decode($pessoa->getDsNome()));
			}
		}

		if(!$mail->Send())
		{
		   return true;
		}
		return false;
	}

	public function tarefaAtribuida($params = array())
	{
		$this->view->assign('tarefa', $params['tarefa']);

		$mail = new \Foxframe\Mail();
		$mail->Subject		= utf8_decode("[Intelihelp] Tarefa #{$params['tarefa']->getIdTarefa()} atribuída");

		if ($params['tarefa']->getResponsavel())
		{
			$this->view->assign('responsavel', "você ({$params['tarefa']->getResponsavel()->getDsNome()})");
			foreach ($params['tarefa']->getResponsavel()->getEmails() as $email)
			{
				$mail->AddAddress($email->getDsEmail(), utf8_decode($params['tarefa']->getResponsavel()->getDsNome()));
			}
		}
		else
		{
			$this->view->assign('responsavel', 'sua equipe');
			$stakeholders = $params['tarefa']->getEquipe()->getPessoas();
			foreach ($stakeholders as $pessoa)
			{
				foreach ($pessoa->getEmails() as $email)
				{
					$mail->AddAddress($email->getDsEmail(), utf8_decode($pessoa->getDsNome()));
				}
			}
		}

		$content	= $this->view->fetch('tarefaAtribuida.html');
		$mail->Body			= $this->view->renderTheme($content);

		if(!$mail->Send())
		{
		   return true;
		}
		return false;
	}

	public function tarefaAvaliar($params = array())
	{
		$this->view->assign('tarefa',		$params['tarefa']);
		$this->view->assign('anotacao',		$params['anotacao']);
		$this->view->assign('pessoa',		$params['pessoa']);
		$this->view->assign('solicitante',	$params['tarefa']->getSolicitante());

		$mail = new \Foxframe\Mail();
		$mail->Subject		= utf8_decode("[Intelihelp] Tarefa #{$params['tarefa']->getIdTarefa()} finalizada");
		$content	= $this->view->fetch('tarefaAvaliar.html');
		$mail->Body			= $this->view->renderTheme($content);

		foreach ($params['tarefa']->getSolicitante()->getEmails() as $email)
		{
			$mail->AddAddress($email->getDsEmail(), utf8_decode($params['tarefa']->getSolicitante()->getDsNome()));
		}

		if(!$mail->Send())
		{
		   return true;
		}
		return false;
	}

	public function tarefaReaberta($params = array())
	{
		$this->view->assign('tarefa', $params['tarefa']);
		$this->view->assign('anotacao', $params['anotacao']);

		$content	= $this->view->fetch('tarefaReaberta.html');
		$mail = new \Foxframe\Mail();
		$mail->Subject		= utf8_decode("[Intelihelp] A Tarefa #{$params['tarefa']->getIdTarefa()} foi reaberta");
		$mail->Body			= $this->view->renderTheme($content);

		$stakeholders = $params['tarefa']->getEquipe()->getPessoas();
		foreach ($stakeholders as $pessoa)
		{
			foreach ($pessoa->getEmails() as $email)
			{
				$mail->AddAddress($email->getDsEmail(), utf8_decode($pessoa->getDsNome()));
			}
		}

		if(!$mail->Send())
		{
		   return true;
		}
		return false;
	}
}